package com.frank.minio.upload.model.entity;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;

/**
 * @author frank
 * @version 1.0
 * @description: TODO
 * @date 2023年09月01日
 */
public class Range {
    private static final Logger log = LoggerFactory.getLogger(Range.class);
    long start;
    long end;
    long length;
    long total;

    /**
     * Range段构造方法.
     *
     * @param start range起始位置.
     * @param end   range结束位置.
     * @param total range段的长度.
     */
    public Range(long start, long end, long total) {
        this.start = start;
        this.end = end;
        this.length = end - start + 1;
        this.total = total;
    }

    public static long sublong(String value, int beginIndex, int endIndex) {
        String substring = value.substring(beginIndex, endIndex);
        return (substring.length() > 0) ? Long.parseLong(substring) : -1;
    }

    public static void copy(InputStream input, OutputStream output, long fileSize, long start, long length) throws IOException {
        //System.out.println("输入流长度：" + input.available());
        byte[] buffer = new byte[1024];
        int len = input.read(buffer);
        int count = 0;
        while (len != -1) {
            count += len;
            output.write(buffer, 0, len);
            len = input.read(buffer);
        }
        System.out.println("输出流长度：" + count);

//        int read = 0;
//        long transmitted = 0;
//        if (fileSize == length) {
//            //需要下载的文件长度与文件长度相同，下载整个文件.
//            while ((transmitted + read) <= length && (read = input.read(buffer)) != -1) {
//                output.write(buffer, 0, read);
//                transmitted += read;
//            }
//            //处理最后不足buff大小的部分
//            if (transmitted < length) {
//                log.info("最后不足buff大小的部分大小为：" + (length - transmitted));
//                read = input.read(buffer, 0, (int) (length - transmitted));
//                output.write(buffer, 0, read);
//            }
//        } else {
//            long toRead = length;
//
//            //如果需要读取的片段，比单次读取的4096小，则使用读取片段大小读取
//            if (toRead < buffer.length) {
//                output.write(buffer, 0, input.read(new byte[(int) toRead]));
//                return;
//            }
//            while ((read = input.read(buffer)) > 0) {
//                toRead -= read;
//                if (toRead > 0) {
//                    output.write(buffer, 0, read);
//                } else {
//                    output.write(buffer, 0, (int) toRead + read);
//                    break;
//                }
//            }
//
//        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Range range = (Range) o;
        return start == range.start &&
                end == range.end &&
                length == range.length &&
                total == range.total;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        return Objects.hash(prime, start, end, length, total);
    }


    public long getStart() {
        return start;
    }

    public void setStart(long start) {
        this.start = start;
    }

    public long getEnd() {
        return end;
    }

    public void setEnd(long end) {
        this.end = end;
    }

    public long getLength() {
        return length;
    }

    public void setLength(long length) {
        this.length = length;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }
}
